"use strict";
/**
 * @name supmail
 * 超级邮件
 */


/**
 * @name submit
 * @param string id 邮件ID
 * 领取附件
 */
exports.submit = function () {
    let ik = 0, id = this.get('id');
    if (!id) {
        return this.error('args empty');
    }
    let api = '/active/', data = { "id": id, "keys": "item" };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return this.error("supmail_config_empty", id)
        }
        this.updater.active.key(id);  //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filter', ret);
    }).then(config => {
        let val = this.updater.active.get(id, 0) || 0;
        if (val > 1) {
            return this.error("supmail_submit_exist", id);
        }
        //加道具
        this.library.call(this, 'shop/parse', config["item"]);
        //写记录
        // let status = this.config.get('mail.submit')||0;
        this.updater.active.set(id, ik, 2, config['ETime'] || 0);
        return this.updater.save();
    })
}



/**
 * @name remove
 * @param string id 邮件ID
 * 删除邮件
 */
exports.remove = function () {
    let ik = 0, id = this.get('id');
    if (!id) {
        return this.error('args empty');
    }
    let api = '/active/', data = { "id": id };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return this.error("supmail_config_empty", id)
        }
        this.updater.active.key(id);  //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filter', ret);
    }).then(config => {
        let val = this.updater.active.get(id, 0) || 0;
        if (val >= 9) {
            return this.success(1);
        }
        let arr = String(config["item"]).split(',');
        if (arr.length >= 3 && val < 1) {
            return this.error("supmail_remove_disable", id)
        }
        //写记录
        this.updater.active.set(id, ik, 9, config['ETime'] || 0);
        return this.updater.save();
    })
}




/**
* @name submitMul
* @param string id 邮件ID,多个邮件以","分割
* 超邮批量领取
*/
exports.submitMul = function () {
    let id = this.get('id', 'string');
    if (!id) {
        return this.error('args empty');
    }
    let _id_list = id.split(",");
    let ik = 0;


    let query = { "cid": 9 };
    if (id) {
        query["_id"] = { "$in": id.split(",") };
    }
    let api = '/active/getAllByType', data = { "query": query, "keys": "item" };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return;
        }
        for (let _i of _id_list) {
            this.updater.active.key(_i);
        }
        //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filterItems', ret);
    }).then(config => {
        //写记录
        for (let _i of _id_list) {
            let val = this.updater.active.get(_i, 0) || 0;
            if (val > 1) {
                return this.error("supmail_submit_exist", id);
            }
            //let status = this.config.get('mail.submit')||0;
            this.updater.active.set(_i, ik, 2, config['ETime'][_i] || 0);
        }
        //加道具
        this.library.call(this, 'shop/parse', config['itemAll']);
        return this.updater.save();
    })
}


/**
* @name removeMul
* @param string id 邮件ID,多个邮件以","分割
* 超邮批量删除
*/
exports.removeMul = function () {
    let id = this.get('id', 'string');
    if (!id) {
        return this.error('args empty');
    }
    let _id_list = id.split(",");
    let ik = 0;

    let query = { "cid": 9 };
    if (id) {
        query["_id"] = { "$in": id.split(",") };
    }
    let api = '/active/getAllByType', data = { "query": query, "keys": "item" };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return;
        }
        for (let _i of _id_list) {
            this.updater.active.key(_i);
        }
        //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filterItems', ret);
    }).then(config => {
        //写记录
        for (let _i of _id_list) {
            let val = this.updater.active.get(_i, 0) || 0;
            if (val >= 9) {
                continue;
            }
            let arr = String(config["item"][_i]).split(',');
            if (arr.length >= 3 && val < 1) {
                return this.error("supmail_remove_disable", _i)
            }
            this.updater.active.set(id, ik, 9, config['ETime'][_i] || 0);
        }
        return this.updater.save();
    })
}



/**
 * @name read
 * @param string id 邮件ID
 * 已读邮件
 */
exports.read = function () {
    let ik = 0, id = this.get('id');
    if (!id) {
        return this.error('args empty');
    }
    let api = '/active/', data = { "id": id };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return this.error("supmail_config_empty", id)
        }
        this.updater.active.key(id);  //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filter', ret);
    }).then(config => {
        let val = this.updater.active.get(id, 0) || 0;
        if (val >= 1) {
            return this.success(1);
        }
        let arr = String(config["item"]).split(',');
        if (arr.length >= 3 && val < 1) {
            return this.error("supmail_remove_disable", id)
        }
        //写记录
        this.updater.active.set(id, ik, 1, config['ETime'] || 0);
        return this.updater.save();
    })
}

/**
* @name readMul
* @param string id 邮件ID,多个邮件以","分割
* 超邮批量已读
*/
exports.readMul = function () {
    let id = this.get('id', 'string');
    if (!id) {
        return this.error('args empty');
    }
    let _id_list = id.split(",");
    let ik = 0;

    let query = { "cid": 1 };
    if (id) {
        query["_id"] = { "$in": id.split(",") };
    }
    let api = '/active/getAllByType', data = { "query": query, "keys": "item" };
    return this.library('spirit/gzone', api, data).then(ret => {
        if (!ret) {
            return;
        }
        for (let _i of _id_list) {
            this.updater.active.key(_i);
        }
        //shop/filter 中会执行一次this.updater.data()
        return this.library.call(this.updater, 'shop/filterItems', ret);
    }).then(config => {
        //写记录
        for (let _i of _id_list) {
            let val = this.updater.active.get(_i, 0) || 0;
            if (val >= 1) {
                continue;
            }
            let arr = String(config["item"][_i]).split(',');
            if (arr.length >= 3 && val < 1) {
                return this.error("supmail_remove_disable", _i)
            }
            this.updater.active.set(id, ik, 1, config['ETime'][_i] || 0);
        }
        return this.updater.save();
    })
}

